
%%

model_name = 'olg_ra_calvo' ;

%%

eval(['dynare ', model_name, '.mod noclearall']) ;

dyn_in.M_  = M_ ;
dyn_in.oo_ = oo_ ;
dyn_in.solve = 1 ;
dyn_in.linearize_around_diff_y = 0 ;
dyn_in.options_ = options_ ;

SET.M_ = dyn_in.M_ ;
SET.oo_ = dyn_in.oo_ ;
SET.options_ = options_ ;

out = dyn_to_str(dyn_in) ;

SET.mats.Q = out.mats.Q ; Q = SET.mats.Q ;
SET.mats.G = out.mats.G ; G = SET.mats.G ;

SET.str_mats.A0 = out.mats.A ;
SET.str_mats.A1 = out.mats.B ;
SET.str_mats.B0 = out.mats.D ;
SET.str_mats.D0 = out.mats.E ;
SET.str_mats.D2 = out.mats.D2 ;
SET.str_mats.Gamma = out.mats.Gamma ;

%% Extract variable details from Dynare output

n_ = M_.endo_nbr ; % Number of endogenous variables
l_ = M_.exo_nbr ;  % Number of exogenous variables
n_ = n_+1 ;        % Constant

SET.variable.n_ = n_ ; 
SET.variable.l_ = l_ ; 
SET.nparam      = M_.param_nbr ;

var_names   = cellstr(M_.endo_names) ;
param_names = cellstr(M_.param_names) ;
exo_var_names = cellstr(M_.exo_names) ;

for ii=1:n_-1
    eval(['SET.variable.',var_names{ii},'=',int2str(ii),';']) ;
end

for ii=1:l_
    eval(['SET.variable.shock.',exo_var_names{ii},'=',int2str(ii),';']) ;
end

for ii=1:M_.param_nbr
    eval(['SET.param_names.',param_names{ii},'=',int2str(ii),';']) ;
end

%% Compute steady-state

SET.y_ss = (eye(n_-1) - Q(1:n_-1,1:n_-1)) \ Q(1:n_-1,end)  ;
SET.y_ss = [SET.y_ss ; 1] ; % Constant

fprintf('\n')
fprintf('\n')
fprintf('Check steady-state... sum of SS and model SS is:   %5.4f', sum(oo_.steady_state-SET.y_ss(1:end-1))) ;
fprintf('\n')
fprintf('\n')
fprintf('This number should be zero')
fprintf('\n')
fprintf('\n')

%% ZLB algorithm parameters

SET.pos_of_i = SET.variable.i ; % Position of interest rate in mod file variable declaration
SET.tr_row   = 1 ; % Row (line number) of Taylor rule in mod file
SET.maxchk   = 40 ; % Check ZLB binding out 200 periods
SET.stoch    = 1 ;   % Shocks each period? For IRF, identical to 0
SET.zlb_val  = 1 ; %M_.params(SET.param_names.zlb_val) ; % Value of ihat at ZLB

% Structural matrices at ZLB
SET.mat_i_f_zlb = SET.str_mats ;

SET.mat_i_f_zlb.A0(SET.tr_row,:) = 0 ;
SET.mat_i_f_zlb.A1(SET.tr_row,:) = 0 ;
SET.mat_i_f_zlb.D0(SET.tr_row,:) = 0 ;
SET.mat_i_f_zlb.B0(SET.tr_row,:) = 0 ;
SET.mat_i_f_zlb.A0(SET.tr_row,SET.pos_of_i) = 1 ;
SET.mat_i_f_zlb.A0(SET.tr_row,end) = -SET.zlb_val ; % Constant

%SET.mat_i_f_zlb.D0(SET.tr_row,:) = 0 ; %ishock goes to zero

SET.mat_init    = SET.str_mats ; % Structural matrices not at ZLB
SET.mat_fin     = SET.str_mats ; % Structural matrices after ZLB

SET.zlb.Qf          = SET.mats.Q ; 
SET.zlb.mat_init    = SET.mat_init ; 
SET.zlb.mat_i_f_zlb = SET.mat_i_f_zlb ;

%% Estimation parameters

SET.Omega = eye(l_) ;

SET.EST.upperT  = 6 ;
SET.EST.kappa   = 0.005 ;
SET.EST.Tbstar  = 12 ;
SET.EST.N       = 10000;
SET.EST.df      = 8 ;
SET.init_Sigma  = 0 ;
dyn_in.solve    = 0 ;
SET.EST.numbersave = 100 ;

SET.tv_str_chg.flag = 0 ; 